Allocate output buffer for typical use, not worst case#49
Merged
Conversation
…creases the buffer size to the max if actually necessary.
There was a problem hiding this comment.
Pull request overview
This PR reduces baseline decoded-audio buffer allocations by sizing the Opus decode buffer for the typical 20ms frame rather than the 120ms worst-case, and only growing the buffer when a larger packet is actually encountered. This helps conserve memory (notably internal RAM on ESP32) while still handling uncommon larger Opus packets.
Changes:
- Replace worst-case decode buffer sizing with
get_decode_buffer_size()(typical for Opus, fixed upper bound for FLAC/PCM). - Add a single retry path in
SyncTask::decode_chunk()that reallocates the decode buffer if the decoder’s size estimate increases mid-stream. - Update decoder documentation/comments to describe the new “buffer size estimate can grow” behavior for Opus.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/sync_task.cpp | Uses the decoder’s current buffer-size estimate and retries decode once after growing the buffer on demand. |
| src/decoder.h | Renames/clarifies the decoded-output sizing API and documents Opus mid-stream growth behavior. |
| src/decoder.cpp | Implements typical-size initial Opus estimate and raises the estimate when OPUS_BUFFER_TOO_SMALL occurs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Don't allocate the worst case scenario size for Opus packets. Only increases the buffer size to the max if actually necessary. aiosendspin seems to only send Opus packets that are 20 ms, so that default should cover the normal use case.
This is beneficial for conserving internal memory, especially on ESP32 where it is very beneficial to put the output buffer in internal memory.